package com.client.mikocompose.ui.modules.home

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.core.view.WindowCompat
import com.client.mikocompose.ui.modules.discover.DiscoverCompose
import com.client.mikocompose.ui.modules.message.MessageCompose
import com.client.mikocompose.ui.modules.user.MineCompose
import com.google.accompanist.insets.ProvideWindowInsets
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.PagerDefaults
import com.google.accompanist.pager.rememberPagerState
import com.linx.common.baseData.themeTypeState
import com.linx.playAndroid.R
import com.linx.playAndroid.ui.theme.*
import com.linx.playAndroid.widget.StatsBarUtil
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * @Description:
 * @Author: 吴祖清
 * @CreateDate: 12.2.22 10:52 上午
 */
class HomeActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val themeState = themeTypeState.value
            WindowCompat.setDecorFitsSystemWindows(window, false)
            StatsBarUtil().StatsBarColor(true, statusBarDarkIcons = true)
            //主题包裹
            CustomThemeManager.WanAndroidTheme(themeState) {
                //可以获取状态栏高度
                ProvideWindowInsets {
                    //主界面
                    ViewPageDemo()
                }
            }
        }
    }

}

@OptIn(ExperimentalPagerApi::class)
@Composable
fun ViewPageDemo() {
    Box {
        val pages by mutableStateOf(
            listOf("首页", "发现", "消息", "我的")
        )
        val pagerState = rememberPagerState(pageCount = pages.size)
        StatsBarUtil().StatsBarColor(true, statusBarDarkIcons = true)
        Column(modifier = Modifier.fillMaxSize()) {
            HorizontalPager(
                state = pagerState,                                     //用于控制或观察viewpage状态的状态对象。
                modifier = Modifier
                    .weight(1f)
                    .background(b1)
            ) { page ->                                                 //页面内容描述
                when (pages[page]) {
                    "首页" -> HomeCompose()
                    "发现" -> DiscoverCompose()
                    "消息" -> MessageCompose()
                    "我的" -> MineCompose()
                }
            }
            Divider()
            TabRow(
                selectedTabIndex = pagerState.currentPage,
                indicator = {},
                backgroundColor = white,
            ) {
                //给全部页面添加标签栏
                pages.forEachIndexed { index, title ->
                    Tab(
                        text = { Text(title) },                          //标签名
                        selected = pagerState.currentPage == index,      //是否选中
                        onClick = {                                      //点击事件
                            CoroutineScope(Dispatchers.Main).launch {
                                pagerState.scrollToPage(index)
                            }
                        },
                        modifier = Modifier.alpha(0.9f),            //透明度
                        enabled = true,                                   //是否启用
                        selectedContentColor = black,                    //选中的颜色
                        unselectedContentColor = grey1,                  //未选中的颜色
                    )
                }
            }
        }
    }
}
